package com.googlecode.cswish.struts.spring;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;

/**
 * Create the service name based on the scan package name
 * 
 * @author Jerry.Feng Date: 2008-7-22
 *
 */
public class DefaultBeanNameGenerator extends AnnotationBeanNameGenerator {
	
	@Override
	public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
		// set lazy model if current environment is development mode
		// FIXME:
		boolean devMode = false;
		if (devMode && definition instanceof AbstractBeanDefinition) {
			// in fact, it because spring only use 'new ScannedGenericBeanDefinition()' to 
			// instance the 'definition' instead of changing the property value according
			// to "<beans>" element of XML			
			AbstractBeanDefinition lazyDef = (AbstractBeanDefinition) definition;
			lazyDef.setLazyInit(true);
		}
		
		return super.generateBeanName(definition, registry);
	}
	
	@Override
	protected String buildDefaultBeanName(BeanDefinition definition) {
		// TODO: Read base package name through 'Config'?
		String className = definition.getBeanClassName();
		return super.buildDefaultBeanName(definition);
	}
}
